package com.example.excelapache_poi.common;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/07/06  13:11
 * @Description: 统一返回结果格式
 */
public class Result<T>{
        //内部状态码，表示流程继续
        public static final int GO_ON = -1;
        public static final int SUCCESS = 0;
        public static final int FAIL = 9;
        public static final int REPEAT = 10;
        // 超时
        public static final int SOCKET_TIME_OUT = 99;
        //用户登录超时
        public static final int USER_TIME_OUT = 999;
        //用户登录超时原因
        public static final String USER_TIME_REASON = "用户登录已失效，请重新登录";

        private int code;
        private String msg;
        private T data;

        public Result(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public Result() {
        }

        public Result(int code, String msg, T data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }

        public Result(ResultEnum resultEnum) {
            this.code = resultEnum.getCode();
            this.msg = resultEnum.getMsg();
        }



    public static Result success() {
            return new Result(SUCCESS, null, null);
        }

        public static <T> Result success(T data) {
            return new Result(SUCCESS, null, data);
        }

        public static <T> Result success(String msg, T data) {
            return new Result(SUCCESS, msg, data);
        }

        public static <T> Result successWithMsg(String msg) {
            return new Result(SUCCESS, msg, null);
        }

        public static <T> Result build(int code, String msg, T data) {
            return new Result(code, msg, data);
        }

        public static Result build(int code) {
            return new Result(code, null, null);
        }

        public static Result build(int code, String msg) {
            return new Result(code, msg, null);
        }

        public static <T> Result build(ResultEnum resultEnum, T data) {
            return new Result(resultEnum.getCode(), resultEnum.getMsg(), data);
        }

        public static Result build(ResultEnum resultEnum) {
            return new Result(resultEnum);
        }


        public static Result fail() {
            return new Result(FAIL, null, null);
        }

        public static Result failWithMsg(String msg) {
            return new Result(FAIL, msg, null);
        }

        public static Result loginFailWithMsg() {
            return new Result(USER_TIME_OUT, USER_TIME_REASON, null);
        }

        public static Result repeatWithMsg(String msg) {
            return new Result(REPEAT, msg, null);
        }

        public static <T> Result fail(T data) {
            return new Result(FAIL, null, data);
        }

        public static <T> Result fail(String msg, T data) {
            return new Result(FAIL, msg, data);
        }

        public static <T> Result fail(int code , String msg) {
            return new Result(code, msg, null);
        }

        public static <T> Result fail(int code, String msg, T data) {
            return new Result(code, msg, data);
        }

        public static <T> Result serverError() {
            return Result.build(ResultEnum.SERVER_ERROR);
        }

        public static <T> Result goOn() {
            return Result.build(GO_ON);
        }

        public static <T> Result repeat() {
            return Result.build(REPEAT);
        }

        public static <T> Result successWithMsgAndData(String msg, T data) {
            return new Result(SUCCESS, msg, data);
        }

        public static <T> Result timeOut() {return Result.build(SOCKET_TIME_OUT, "网络超时");}

        public static <T> Result timeOut(String msg) {return Result.build(SOCKET_TIME_OUT, msg);}

        @JsonIgnore
        public boolean isSuccess() {
            return SUCCESS == code;
        }

        @JsonIgnore
        public boolean isGoOn() {
            return GO_ON == code;
        }

        @JsonIgnore
        public boolean repeatCancel() {
            return REPEAT == code;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        @Override
        public String toString() {
            return "Result{" +
                    "code=" + code +
                    ", msg='" + msg + '\'' +
                    ", data=" + data +
                    '}';
        }

        public boolean isTimeOut() {
            return SOCKET_TIME_OUT == code;
        }
}
